package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dao.SmsFlashPromotionProductRelationDao;
import com.macro.mall.dto.SmsFlashPromotionProduct;
import com.macro.mall.mapper.mapper.SmsFlashPromotionProductRelationMapper;
import com.macro.mall.model.SmsFlashPromotionProductRelation;
import com.macro.mall.model.SmsFlashPromotionProductRelationExample;
import com.macro.mall.model.SmsFlashPromotionSkuSort;
import com.macro.mall.service.SmsFlashPromotionProductRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

/**
 * 限时购商品关联管理Service实现类
 * Created by macro on 2018/11/16.
 */
@Service
public class SmsFlashPromotionProductRelationServiceImpl implements SmsFlashPromotionProductRelationService {
    @Autowired
    private SmsFlashPromotionProductRelationMapper relationMapper;
    @Autowired
    private SmsFlashPromotionProductRelationDao relationDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public int create(List<SmsFlashPromotionProductRelation> relationList) {
        for (SmsFlashPromotionProductRelation relation : relationList) {
            relationMapper.insert(relation);
        }
        return relationList.size();
    }
    @Transactional
    @Override
    public int update(Long id, SmsFlashPromotionSkuSort relation) {

        // 设置传入的 ID
        relation.setId(id);

        // 根据 ID 查询活动信息及场次信息
        SmsFlashPromotionProductRelation smsFlashPromotionProductRelation = relationMapper.selectByPrimaryKey(id);

        if (smsFlashPromotionProductRelation == null) {
            throw new RuntimeException("活动信息场次不存在");
        }

        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

       // 获取活动的开始和结束时间，并转换为LocalDateTime
        LocalDateTime promotionStartTime = smsFlashPromotionProductRelation.getPromotionStartTime()
                .toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

        LocalDateTime promotionEndTime = smsFlashPromotionProductRelation.getPromotionEndTime()
                .toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

       // 校验当前时间是否在抢购活动的日期范围内
        if (now.isBefore(promotionStartTime) || now.isAfter(promotionEndTime)) {
            throw new RuntimeException("当前时间不在抢购活动的有效时间范围内");
        }

        // 根据 relation 的 SKU ID 及活动 ID、场次 ID 组合条件删除旧的 SKU 排序记录
        SmsFlashPromotionSkuSort deleteCondition = new SmsFlashPromotionSkuSort();
        deleteCondition.setFlashPromotionId(smsFlashPromotionProductRelation.getFlashPromotionId());
        deleteCondition.setFlashPromotionSessionId(smsFlashPromotionProductRelation.getFlashPromotionSessionId());
        deleteCondition.setSkuId(relation.getSkuId()); // 假设 relation 中包含 skuId
        deleteCondition.setFlashPromotionPrice(relation.getFlashPromotionPrice());
        deleteCondition.setFlashPromotionCount(relation.getFlashPromotionCount());
        deleteCondition.setFlashPromotionLimit(relation.getFlashPromotionLimit());

        relationDao.delete(deleteCondition);

        // 组装信息，批量保存至 sms_flash_promotion_sku_sort 表中
        List<SmsFlashPromotionSkuSort> newSkuSorts = new ArrayList<>();
        // 假设这里需要添加新的 SKU 排序记录，具体逻辑需根据实际需求进行调整
        SmsFlashPromotionSkuSort newSkuSort = new SmsFlashPromotionSkuSort();
        newSkuSort.setFlashPromotionId(relation.getFlashPromotionId());
        newSkuSort.setFlashPromotionSessionId(relation.getFlashPromotionSessionId());
        newSkuSort.setFlashPromotionPrice(relation.getFlashPromotionPrice());
        newSkuSort.setFlashPromotionCount(relation.getFlashPromotionCount());
        newSkuSort.setFlashPromotionLimit(relation.getFlashPromotionLimit());
        newSkuSort.setSort(relation.getSort());
        newSkuSorts.add(newSkuSort);


        if (!newSkuSorts.isEmpty()) {
            relationDao.batchInsert(newSkuSorts);
        }





        // 更新活动信息
        return relationMapper.updateByPrimaryKey(relation);
    }

    @Override
    public int delete(Long id) {
        return relationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SmsFlashPromotionProductRelation getItem(Long id) {
        return relationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SmsFlashPromotionProduct> list(Long flashPromotionId, Long flashPromotionSessionId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        return relationDao.getList(flashPromotionId,flashPromotionSessionId);
    }

    @Override
    public long getCount(Long flashPromotionId, Long flashPromotionSessionId) {
        SmsFlashPromotionProductRelationExample example = new SmsFlashPromotionProductRelationExample();
        example.createCriteria()
                .andFlashPromotionIdEqualTo(flashPromotionId)
                .andFlashPromotionSessionIdEqualTo(flashPromotionSessionId);
        return relationMapper.countByExample(example);
    }
}
